﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Jungle.Code.DAL;
using Jungle.Code.Utils;
using System.Data.SqlClient;
using System.Data;

namespace Jungle.Code.ProductManager
{
    public class ProductType
    {

        public string err;
        private string ConnectionString = DBHelper.ConnectionString;

        public List<ProductType> ProductTypeList;

        #region Fields
        private int typeId;
        private string typeDescription;
        #endregion

        #region Properties
        public int TypeId { get { return typeId; } set { typeId = value; } }
        public string TypeDescription { get { return typeDescription; } set { typeDescription = value; } }
        #endregion

        #region Functions

        //-------------------------------------------------------------------------------------------
        public void Add()
        {
            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("catalog.ProductType_Add", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@TypeDescription", SqlDbType.VarChar).Value = TypeDescription;
                
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            }
        }
        //-------------------------------------------------------------------------------------------
        public void Delete()
        {
            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("catalog.ProductType_Delete", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@TypeId", SqlDbType.Int).Value = TypeId;

                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            } 
        }
        //-------------------------------------------------------------------------------------------
        public int GetAll()
        {
            int count = 0;
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                ProductTypeList = new List<ProductType>();
                SqlCommand command = new SqlCommand("catalog.ProductType_GetAll", connect);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    ProductType p = new ProductType();
                    
                    p.TypeId = (int)reader["TypeId"];
                    p.TypeDescription = reader["TypeDescription"].ToString().Trim();

                    ProductTypeList.Add(p);
                    count++;
                }

                reader.Close();
                command.Connection.Close();

            }
            return count; 
        }
        //-------------------------------------------------------------------------------------------
        public void GetById()
        {
            SqlConnection connect = new SqlConnection(ConnectionString);
            SqlCommand command = new SqlCommand("catalog.ProductType_GetById", connect);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                command.Parameters.Add("@TypeId", SqlDbType.Int).Value = TypeId;
                SqlDataReader reader = null;
                command.Connection.Open();
                
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    TypeDescription = reader["TypeDescription"].ToString().Trim();
                }

            }
            catch (SqlException ex)
            {
                err = ex.ToString();

            }
            finally
            {
                command.Connection.Close();
            }
        }

        #endregion

    }
}
